package com.yangfei.mystudy1020.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.yangfei.mystudy1020.R;
import com.yangfei.mystudy1020.utils.Network;
import com.yangfei.mystudy1020.utils.SafeHandlerBase;

import java.util.ArrayList;

import top.cuihp.serverlibrary.client.ClientConfig;
import top.cuihp.serverlibrary.client.MinaClient;

public class MinaClientActivity extends AppCompatActivity {
    final String TAG = MinaClientActivity.class.getSimpleName();

    static private SafeHandler mMainHandler;
    // 连接 断开连接 发送数据到服务器 的按钮变量
    private Button btnConnect, btnDisconnect, btnSend;

    // 显示接收服务器消息 按钮
    private TextView receive_message;

    // 输入需要发送的消息 输入框
    private EditText mEdit;

    MinaClient  minaClient;
    String response;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mina_client);

        String ipList = Network.getWifiApIpAddress();

        mMainHandler = new SafeHandler(this);
        // 初始化所有按钮
        btnConnect = (Button) findViewById(R.id.connect);
        btnDisconnect = (Button) findViewById(R.id.disconnect);
        btnSend = (Button) findViewById(R.id.send);
        mEdit = (EditText) findViewById(R.id.edit);
        receive_message = (TextView) findViewById(R.id.receive_message);

        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                init();
            }
        });

        btnDisconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                minaClient.disConnect();
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = mEdit.getText().toString();
                minaClient.sendMessage(msg);
            }
        });
    }

    private void init(){
        //建造者模式进行相关配置
        ClientConfig clientConfig = new ClientConfig.Builder().setIp("192.168.43.1").setPort(8091).build();
        minaClient = new MinaClient(clientConfig);
        //状态进行监听
        minaClient.setClientStateListener(new MinaClient.ClientStateListener() {
            @Override
            public void sessionCreated() {
                Log.e(TAG, "client sessionCreated ");
            }

            @Override
            public void sessionOpened() {
                Log.e(TAG, "client sessionOpened ");
            }

            @Override
            public void sessionClosed() {
                Log.e(TAG, "client sessionClosed ");
            }

            @Override
            public void messageReceived(String message) {
                Log.e(TAG, "client messageReceived "+ message);
                response = message;
                if(response!=null){
                    // 步骤4:通知主线程,将接收的消息显示到界面
                    Message msg = Message.obtain();
                    msg.what = 0;
                    mMainHandler.sendMessage(msg);
                }
            }

            @Override
            public void messageSent(String message) {
                Log.e(TAG, "client messageSent "+message);

            }
        });
    }

    static class SafeHandler extends SafeHandlerBase {
        public SafeHandler(Activity act) {
            super(act);
        }

        @Override
        public void handleMessage(Message msg) {
            if (checkRef()) {
                MinaClientActivity sAct = (MinaClientActivity) mRefActivity.get();
                switch (msg.what) {
                    case 0:
                        sAct.receive_message.setText(sAct.response);
                        break;
                }
            }
        }
    }

}
